//app.js
App({
  //api_root: 'https://bill.dgyoubei.com/',
  api_root: 'http://www.jizhangben.cc/',
  globalData: {
    web_info:{
      web_name:'优倍记账本',
      version:'v1.0.0'
    },
  },

  onLaunch: function() {
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let capsule = wx.getMenuButtonBoundingClientRect();
		if (capsule) {
		 	this.globalData.Custom = capsule;
			this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
		} else {
			this.globalData.CustomBar = e.statusBarHeight + 50;
		}
      }
    })
  },
  

  _get: function(url, data, success, fail, complete) {
    let App = this;
    data = data || {};
    data.token = wx.getStorageSync('token');
    wx.request({
      url: App.api_root + url,
      data: data,
      success: function(res) {
        if (res.statusCode !== 200) {
          App.showError('网络请求出错');
          return false;
        }else {
          success && success(res.data);
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    });
  },

  /**
   * post提交
   */
  _post: function(url, data, success, fail, complete) {
    let App = this;
    data.token = wx.getStorageSync('token');
    wx.request({
      url: App.api_root + url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: data,
      success: function(res) {
        if (res.statusCode !== 200) {
          App.showError('网络请求出错');
          return false;
        }
        success && success(res.data);
      },
      fail: function(res) {
        App.showError(res.errMsg, function() {
          fail && fail(res);
        });
      },
      complete: function(res) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        complete && complete(res);
      }
    });
  },

  /* 显示成功提示框 */
  showSuccess: function(msg, callback) {
    wx.showToast({
      title: msg,
      icon: 'success',
      success: function() {
        callback && (setTimeout(function() {
          callback();
        }, 1500));
      }
    });
  },

  /* 显示失败提示框  */
  showError: function(msg, callback) {
    wx.showModal({
      title: '友情提示',
      content: msg,
      showCancel: false,
      success: function(res) {
        callback && callback();
      }
    });
  },
})
